import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:top_news/NewListBuilder.dart';
import 'package:top_news/models/NewsResponse.dart';
import 'package:top_news/utils/NetUtil.dart';

import 'models/Channel.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: '头条新闻'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  MyHomePage({Key key, this.title}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

List<Channel> _channelList = List();

class _MyHomePageState extends State<MyHomePage> {
  List<Widget> listItems;
  Channel currentChannel;

  @override
  void initState() {
    super.initState();
    _channelList.add(Channel("top", "头条"));
    _channelList.add(Channel("shehui", "社会"));
    _channelList.add(Channel("guonei", "国内"));
    _channelList.add(Channel("guoji", "国际"));
    _channelList.add(Channel("yule", "娱乐"));
    _channelList.add(Channel("tiyu", "体育"));
    _channelList.add(Channel("junshi", "军事"));
    _channelList.add(Channel("keji", "科技"));
    _channelList.add(Channel("caijing", "财经"));
    _channelList.add(Channel("shishang", "时尚"));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: listItems == null
              ? Text("点击刷新按钮刷新新闻")
              : ListView(
                  children: listItems,
                )),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.all(0),
          children: buildDrawerItems(),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          refreshNews();
        },
        tooltip: 'refresh',
        child: Icon(
          Icons.refresh,
          size: 30,
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  List<Widget> buildDrawerItems() {
    List<Widget> widgets = List();
    widgets.add(buildDrawerHeader());
    for (int i = 0; i < _channelList.length; i++) {
      widgets.add(InkWell(
        child: Container(
          padding: EdgeInsets.all(10),
          child: Text(
            _channelList[i].name,
            style: TextStyle(
              color: Colors.blue,
              fontSize: 20,
            ),
            textAlign: TextAlign.center,
          ),
        ),
        onTap: () {
          currentChannel = _channelList[i];
          refreshNews();
        },
      ));
    }
    return widgets;
  }

  Widget buildDrawerHeader() {
    return Container(
      child: Text(
        "频道列表",
        style: TextStyle(
            color: Colors.white, fontSize: 26, fontWeight: FontWeight.w500),
        textAlign: TextAlign.end,
      ),
      height: 160,
      color: Colors.blue,
      padding: EdgeInsets.all(5),
      alignment: Alignment.bottomCenter,
    );
  }

  refreshNews() async {
    String url =
        "${Host.toutiao}?type=${currentChannel != null ? currentChannel.type : ""}&key=95d252f67a509224504f3ab7e935b27e";
    debugPrint(url);
    String newsResponseContent = await NetUtil(url).pullData();
    debugPrint(newsResponseContent);
    NewsResponse newsResponse =
        NewsResponse.fromJson(json.decode(newsResponseContent));
    listItems = NewListBuilder.genWidgetsFromJson(newsResponse);
    setState(() {});
  }
}
